$(function() {
    //
	// var userid = sessionStorage['userid'];
    $("#logOut").click(function () {
        sessionStorage.setItem('username','')
    });
    $("#out").click(function () {
        $("#logOut").click();
    });
    var username = sessionStorage['username'];
	// var UserType = sessionStorage['usertype'];
	//
	if (!username){
        window.location.href = "adminlogin.html";
        alert("请先登录！");
	}
	//
	// //f5刷新，刷新当前页面
	$("body").bind("keydown", function(event) {
		if (event.keyCode == 116) {
			event.preventDefault(); //阻止默认刷新
			//location.reload();
			//采用location.reload()在火狐下可能会有问题，火狐会保留上一次链接
			location = location;
		}
	});
	// 旧密码失焦验证
	$("#oldPassword").blur(function () {
		var password = $("#oldPassword").val();
		if (!password.trim()) {
            $('.name-old').removeClass('hide').find("em").text('原密码不能为空');
            $('.name-old').find("i").attr('class', 'icon-warn').css("color", "#ea5d5f");
            return;
		}else{
            $('.name-old').addClass("hide");
		}

    });
	// 点击确认修改密码提交
	$("#submitMenu").click(function () {
		var oldPass = $("#oldPassword").val();
		var newPass = $("#newPassword").val();
		var confirmPassword = $("#confirmPassword").val();
        console.log("username:"+username);
        if (!newPass.trim()) {
            $('.name-new').removeClass('hide').find("em").text('新密码不能为空');
            $('.name-new').find("i").attr('class', 'icon-warn').css("color", "#ea5d5f");
            return;
        }
        if (!newPass.trim()) {
            $('.name-new').removeClass('hide').find("em").text('新密码不能为空');
            $('.name-new').find("i").attr('class', 'icon-warn').css("color", "#ea5d5f");
            return;
        }else {
            $('.name-new').addClass("hide");
		}
        if (newPass.trim() !== confirmPassword.trim()) {
            $('.name-con').removeClass('hide').find("em").text('两次密码不一致');
            $('.name-con').find("i").attr('class', 'icon-warn').css("color", "#ea5d5f");
            return;
        }else {
            $('.name-con').addClass("hide");
		}
            $.ajax({
			url: "/sys/verifyPwd",
			// url: "http://localhost:8080/sys/verifyPwd",
			type: "post",
			data: {
                username: username,
                oldPwd: oldPass,
                newPwd: newPass
			},
			success: function (data) {
                console.log(data);
                if (data.code == 1 && data.msg != "") {
                    alert("密码修改成功！");
                    // sessionStorage.setItem('username','');
                    $("#myModal").modal("hide");

                }
            },
			error: function (error) {
                console.log(error);
            }
		})
    })
});


